package com.itcast.flink.eventtime;

import lombok.SneakyThrows;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Date;

/**
 * Flink Window窗口计算：基于事件时间窗口EventTimeWindow，此处滚动时间窗口
 * todo: 要求数据中必须自带数据产生的时间字段，也就是数据事件时间，将流式数据划分为窗口时，依据事件时间划分
 *
 * @author lilulu
 */
public class EventTimeWindowWatermarkDemo {
    public static void main(String[] args) throws Exception {
        // 1. 执行环境-env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 2. 数据源-source
        DataStreamSource<String> inputStream = env.socketTextStream("node1", 9999);
        /**
         2022-04-01 09:00:01,a,1
         2022-04-01 09:00:02,a,1
         2022-04-01 09:00:05,a,1
         2022-04-01 09:00:04,a,1

         2022-04-01 09:00:07,a,1

         2022-04-01 09:00:12,a,1
         */
        // 3. 数据转换-transformation
        // 3-1. 过滤脏数据，并且指定数据中事件时间字段的值（必须转换为Long类型）
        // 3-2. 对数据进行解析封装操作，获取卡口名称和卡口流量，放入二元组中
        // 3-3. 设置滚动事件时间窗口，进行窗口数据计算
        SingleOutputStreamOperator<String> timeStream = inputStream.filter(line -> line.trim().split(",").length == 3)
                .assignTimestampsAndWatermarks(
                        // 考虑数据乱序问题,设置允许最大乱序时间，也就是等待时间：2s (为了测试设置这么大）
                        WatermarkStrategy.<String>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                                .withTimestampAssigner(new SerializableTimestampAssigner<String>() {
                                    private FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

                                    @SneakyThrows
                                    @Override
                                    public long extractTimestamp(String element, long recordTimestamp) {
                                        System.out.println("element: " + element);
                                        String[] split = element.split(",");
                                        String eventTime = split[0];
                                        Date eventDate = fastDateFormat.parse(eventTime);
                                        return eventDate.getTime();
                                    }
                                })
                );
        SingleOutputStreamOperator<Tuple2<String, Integer>> mapStream = timeStream.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] split = value.split(",");
                return Tuple2.of(split[1], Integer.parseInt(split[2]));
            }
        });

        SingleOutputStreamOperator<String> windowStream = mapStream.keyBy(tuple -> tuple.f0)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .apply(new WindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>() {
                    private FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

                    @Override
                    public void apply(String key, TimeWindow timeWindow, Iterable<Tuple2<String, Integer>> iterable, Collector<String> collector) throws Exception {
                        String windowStart = this.format.format(timeWindow.getStart());
                        String windowEnd = this.format.format(timeWindow.getEnd());
                        int sum = 0;
                        for (Tuple2<String, Integer> tuple2 : iterable) {
                            sum += tuple2.f1;
                        }
                        String output = "window: [" + windowStart + " ~ " + windowEnd + "], " + key + " = " + sum;
                        collector.collect(output);
                    }
                });

        // 4. 数据终端-sink
        windowStream.printToErr();
        // 5. 触发执行-execute
        env.execute("EventTimeWindowDemo");
    }
}